﻿using FOModels.EducationLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.EducationLoan
{
    public class EducationLoanDAL
    {
        private SQLDataAccess db;
        public EducationLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from EducationLoan order by EducationLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from EducationLoan sa join EducationLoanLang hl on sa.EducationLoanID = hl.EducationLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getEducationLoanrelati(int CompanyID, int EducationLoanID, int langid)
        {
            return db.exeSelect("select * from EducationLoan ca join EducationLoanLang hl on ca.EducationLoanID = hl.EducationLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.EducationLoanID != " + EducationLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getEducationLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from EducationLoan sa join EducationLoanLang hl on sa.EducationLoanID = hl.EducationLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableEducationLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getEducationLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from EducationLoan sa join EducationLoanLang hl on sa.EducationLoanID = hl.EducationLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableEducationLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getEducationLoanDefault(int langid)
        {
            return db.exeSelect("Select * from EducationLoan sa join EducationLoanLang hl on sa.EducationLoanID = hl.EducationLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableEducationLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getEducationLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from EducationLoan sa join EducationLoanLang hl on sa.EducationLoanID = hl.EducationLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableEducationLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getEducationLoanByID(int EducationLoanID, int langid)
        {
            return db.exeSelect("select * from EducationLoan ca join EducationLoanLang hl on ca.EducationLoanID = hl.EducationLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.EducationLoanID = " + EducationLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getEducationLoanByIDManager(int EducationLoanID, int langid)
        {
            return db.exeSelect("select * from EducationLoan ca join EducationLoanLang hl on ca.EducationLoanID = hl.EducationLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.EducationLoanID = " + EducationLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateEducationLoan(EducationLoanModels models)
        {
            return db.exeUpdate("update EducationLoan set EducationLoanName = N'" + models.EducationLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where EducationLoanID = " + models.EducationLoanID + "");
        }
        public int InsertEducationLoan(EducationLoanModels models)
        {
            return db.exeUpdate("insert into EducationLoan values (N'" + models.EducationLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteEducationLoan(int EducationLoanID)
        {
            return db.exeUpdate("Delete from EducationLoan where EducationLoanID = " + EducationLoanID + "");
        }
        public int UpdateEducationLoanLang(EducationLoanModels models, int langid)
        {
            return db.exeUpdate("update EducationLoanLang set EducationLoanName = N'" + models.EducationLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where EducationLoanID = " + models.EducationLoanID + " and LangID=" + langid + "");
        }
        public int InsertEducationLoanLang(EducationLoanModels models, int langid)
        {
            return db.exeUpdate("insert into EducationLoanLang values (" + models.EducationLoanID + "," + langid + ",N'" + models.EducationLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteEducationLoanLang(int EducationLoanID)
        {
            return db.exeUpdate("Delete from EducationLoanLang where EducationLoanID = " + EducationLoanID + "");
        }

        public int DeleteEducationLoanApply(int EducationLoanApplyID)
        {
            return db.exeUpdate("Delete from EducationLoanApply where EducationLoanApplyID = " + EducationLoanApplyID + "");
        }
        public DataTable getcontractbyid(int EducationLoanapplyid)
        {
            return db.exeSelect("select * from EducationLoanApply ca join [EducationLoan] c on ca.EducationLoanID = c.EducationLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.EducationLoanApplyID = " + EducationLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from EducationLoanApply ca join [EducationLoan] c on ca.EducationLoanID = c.EducationLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from EducationLoanApply ca join [EducationLoan] c on ca.EducationLoanID = c.EducationLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int EducationLoanApply(int EducationLoanID, EducationLoanApplyModels app)
        {
            return db.exeUpdate("insert into EducationLoanApply(EducationLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + EducationLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(EducationLoanApplyModels EducationLoan)
        {
            return db.exeUpdate("update EducationLoanApply set StatusID= " + EducationLoan.StatusID + " where EducationLoanApplyID = " + EducationLoan.EducationLoanApplyID + "");
        }
    }
}
